package com.poxz.algorithm.template;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 排序操作
 *
 * @author poxz
 */
public class SortingExamples {

    /**
     * 基础排序
     */
    static void basicSort() {
        int[] arr = {5, 2, 8, 1, 9};
        Arrays.sort(arr);
        System.out.println("数组排序: " + Arrays.toString(arr));

        List<Integer> list = new ArrayList<>(Arrays.asList(5, 2, 8, 1, 9));
        Collections.sort(list);
        System.out.println("List排序: " + list);
        System.out.println();
    }

    /**
     * 降序排序
     */
    static void reverseSort() {
        Integer[] arr = {5, 2, 8, 1, 9};
        Arrays.sort(arr, (a, b) -> b - a);
        System.out.println("数组降序: " + Arrays.toString(arr));

        List<Integer> list = new ArrayList<>(Arrays.asList(5, 2, 8, 1, 9));
        list.sort((a, b) -> b - a);
        System.out.println("List降序: " + list);

        List<Integer> list2 = new ArrayList<>(Arrays.asList(5, 2, 8, 1, 9));
        Collections.sort(list2, Collections.reverseOrder());
        System.out.println("List降序2: " + list2);
        System.out.println();
    }

    /**
     * 自定义排序
     */
    static void customSort() {
        Integer[] arr = {-5, 2, -8, 1, 9};
        Arrays.sort(arr, (a, b) -> Math.abs(a) - Math.abs(b));
        System.out.println("按绝对值: " + Arrays.toString(arr));

        Integer[] arr2 = {1, 2, 3, 4, 5, 6};
        Arrays.sort(arr2, (a, b) -> {
            if (a % 2 != b % 2) {
                return b % 2 - a % 2;
            } else {
                return a - b;
            }
        });
        System.out.println("奇数在前: " + Arrays.toString(arr2));
        System.out.println();
    }

    /**
     * 数组区间排序
     */
    static void rangeSort() {
        int[] arr = {5, 2, 8, 1, 9, 3};
        Arrays.sort(arr, 1, 4);
        System.out.println("区间排序: " + Arrays.toString(arr));
        System.out.println();
    }

    /**
     * 对象排序
     */
    static void objectSort() {
        class Student {
            String name;
            int score;

            Student(String name, int score) {
                this.name = name;
                this.score = score;
            }

            @Override
            public String toString() {
                return name + ":" + score;
            }
        }

        List<Student> students = new ArrayList<>();
        students.add(new Student("Alice", 85));
        students.add(new Student("Bob", 92));
        students.add(new Student("Charlie", 78));

        students.sort((a, b) -> a.score - b.score);
        System.out.println("按分数: " + students);

        students.sort((a, b) -> a.name.compareTo(b.name));
        System.out.println("按名字: " + students);
        System.out.println();
    }

    /**
     * 多关键字排序
     */
    static void multiKeySort() {
        int[][] people = {
                {85, 20},
                {92, 18},
                {85, 22},
                {78, 19}
        };

        Arrays.sort(people, (a, b) -> {
            if (a[0] != b[0]) {
                return b[0] - a[0];
            } else {
                return a[1] - b[1];
            }
        });

        System.out.println("多关键字排序:");
        for (int[] p : people) {
            System.out.println("  分数:" + p[0] + ", 年龄:" + p[1]);
        }
        System.out.println();
    }

    /**
     * 按绝对值排序
     *
     * @param a 整数数组
     */
    static void sortByAbs(Integer[] a) {
        Arrays.sort(a, (x, y) -> Math.abs(x) - Math.abs(y));
    }

    /**
     * 二维数组按第一维排序
     *
     * @param a 二维数组
     */
    static void sort2D(int[][] a) {
        Arrays.sort(a, (x, y) -> x[0] - y[0]);
    }

    /**
     * 功能测试
     *
     * @param args 命令行参数
     */
    public static void main(String[] args) {
        System.out.println("=== 排序示例 ===\n");

        basicSort();
        reverseSort();
        customSort();
        rangeSort();
        objectSort();
        multiKeySort();
    }
}
